using UnityEngine;

/// <summary>
/// 建筑管理系统（单例模式）
/// 负责处理建筑的选择、放置逻辑
/// </summary>
public class BuildManager : MonoBehaviour
{
    public static BuildManager Instance { get; private set; }

    [Header("当前建造对象")]
    [Tooltip("当前正在放置的建筑对象")]
    private GameObject curObject;

    [Header("建造状态")]
    [Tooltip("标记是否处于建造模式")]
    [HideInInspector]
    public bool isBuilding;

    private RaycastHit hitInfo;
    public LayerMask groundMask;
    public Material virtualMaterial;//虚影材质

    private void Awake()
    {
        Instance = this;
        isBuilding = false;
    }

    void Update()
    {
        //建筑跟随鼠标
        if (curObject)
        {
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, 1000, groundMask))
                curObject.transform.position = hitInfo.point;

            if (Input.GetMouseButtonDown(0))
            {
                curObject.GetComponent<Buildable>().StartBuild();

                isBuilding = false;
                curObject = null;
            }
        }
        
    }

    /// <summary>
    /// 选择要建造的建筑
    /// </summary>
    /// <param name="prefab">建筑预制体</param>
    public void SelectBuilding(GameObject prefab) // 方法名首字母建议大写
    {
        // 如果已经在建造模式，则不再响应
        if (isBuilding) return;

        // 进入建造状态
        isBuilding = true;

        // 实例化选中的建筑预制体
        curObject = Instantiate(prefab);
    }
}